package com.framework.web.bus.controller;


import com.framework.core.domain.AjaxJson;
import com.framework.core.easyui.domain.DataGridModel;
import com.framework.core.easyui.util.PageUtil;
import com.framework.core.util.MyBeanUtils;
import com.framework.web.bus.domain.Order;
import com.framework.web.bus.domain.OrderItemInfo;
import com.framework.web.bus.service.OrderItemInfoService;
import com.framework.web.bus.service.OrderService;
import com.framework.web.common.domain.BusinessConstants;
import com.framework.web.common.vo.OrderInfoVo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
* @Title: Controller
* @Description: 控制层
* @author zqtao
* @date 2016-09-11
* @version V1.0
*
*/
@Controller
@RequestMapping("/order")
public class OrderController {

private Logger logger = LoggerFactory.getLogger(this.getClass());


/**
* Spring注入资源信息模块service
*/
@Autowired
private OrderService orderService;

@Autowired
private OrderItemInfoService orderItemInfoService;


/**
*
* 分页
* @param dgm
* @param order
* @return
* @throws Exception
*/
@RequestMapping(value = "/list", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> list(DataGridModel dgm, Order order, HttpServletRequest request) throws Exception {
    PageRequest pr = DataGridModel.getPageRequest(dgm);
    return PageUtil.getPageMap(orderService.getPageList(order, pr));
}

/**
*  请求新增或修改页面
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/toSaveAndUpdate/{id}", method = RequestMethod.GET)
public ModelAndView toSaveAndUpdate(@PathVariable("id") String id)
throws Exception {
    ModelAndView modelAndView = new ModelAndView("order/orderForm");
    Order order = new Order();
    if (StringUtils.isNotEmpty(id) && !"0".equals(id)) {
        order = orderService.findById(id);
    } else {

    }
    modelAndView.addObject("order", order);
    return modelAndView;
}


/**
* 保存信息
*
* @param order  信息实体
* @return
* @throws Exception
*/
@RequestMapping(value = "/saveAndUpdate", method = RequestMethod.POST)
@ResponseBody
public AjaxJson saveAndUpdate(HttpServletRequest request, HttpServletResponse response, Order order) throws Exception {
    AjaxJson j = new AjaxJson();
    if (StringUtils.isNotEmpty(order.getId())) {//更新
        Order r = orderService.findById(order.getId());
        MyBeanUtils.copyBeanNotNull2Bean(order, r);
        order = r;
    }else {

    }
    orderService.save(order);
    j.setMsg("保存成功");
    return j;
}

/**
* 根据id字符串删除数据信息
*
* @param id
* @return
* @throws Exception
*/
@RequestMapping(value ="/delete", method = RequestMethod.POST)
@ResponseBody
public AjaxJson delete(String id) throws Exception {
    AjaxJson j = new AjaxJson();
    orderService.delete(id);
    j.setMsg("删除成功");
    return j;
}

/**
 * 订单详情查询
 * @param orderId
 * @return
 */
@RequestMapping(value ="/details/{orderId}", method = RequestMethod.GET)
@ResponseBody
public ModelAndView  findByOrderId(HttpServletRequest request,HttpServletResponse response,@PathVariable("orderId")String orderId){
    ModelAndView mav = new ModelAndView("/bus/orderItemInfo/orderItemInfoForm");
    OrderInfoVo orderInfoVo = new OrderInfoVo();
    Order order = orderService.findById(orderId);
    List<OrderItemInfo> orderItemInfos = orderItemInfoService.findByOrderId(orderId);
    orderInfoVo.setOrder(order);
    orderInfoVo.setOrderItemInfos(orderItemInfos);
    mav.addObject("orderInfoVo",orderInfoVo);
    return mav;
}

/**
 * 订单转发到发货页面
 *
 * @param id
 * @return
 * @throws Exception
 */
@RequestMapping(value ="/toShipments/{id}", method = RequestMethod.GET)
@ResponseBody
public ModelAndView toShipments(@PathVariable("id") String id)
        throws Exception {
    ModelAndView modelAndView = new ModelAndView("/bus/order/orderForm");
    Order order = new Order();
    if (StringUtils.isNotEmpty(id) && !"0".equals(id)) {
        order = orderService.findById(id);
    } else {

    }
    modelAndView.addObject("order", order);
    return modelAndView;
}

/**
 * 保存信息
 *
 * @param order  信息实体
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/shipments", method = RequestMethod.POST)
@ResponseBody
public AjaxJson shipments(HttpServletRequest request, HttpServletResponse response, Order order) throws Exception {
    AjaxJson j = new AjaxJson();
    if (StringUtils.isNotEmpty(order.getId())) {//更新
        Order r = orderService.findById(order.getId());
        MyBeanUtils.copyBeanNotNull2Bean(order, r);
        order = r;
    }else {

    }
    if(order.getOrderStatus()!=BusinessConstants.ORDER_STATUS.NON_SHIPMENTS){
        j.setMsg("发货失败");
    }else {
        order.setOrderStatus(BusinessConstants.ORDER_STATUS.NON_ACCEPTANCE);
        orderService.save(order);
        j.setMsg("发货成功");
    }
    return j;
}


/**
 * 订单取消控制层
 *
 * @param id
 * @return
 * @throws Exception
 */
@RequestMapping(value ="/cancel", method = RequestMethod.POST)
@ResponseBody
public AjaxJson cancel(String id) throws Exception {
    AjaxJson j = new AjaxJson();
    Order order = orderService.findById(id);
    if(order==null){
        j.setMsg("订单不存在");
        return j;
    }
    order.setOrderStatus(BusinessConstants.ORDER_STATUS.INVALID);
    orderService.save(order);
    j.setMsg("订单取消成功");
    return j;
}

}